package com.aier.user.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.user.entity.Notice;

public class NoticeRowMapper implements RowMapper<Notice> {

    @SuppressWarnings("unused")
    private int type;
    
    public static final int FOLLOW_BRIEF=1;

    public NoticeRowMapper() {
    }

    public NoticeRowMapper(int type) {
        this.type = type;
    }

    @Override
    public Notice mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseHome(new Notice(), rs);
    
    }

    public static Notice parseHome(Notice h, ResultSet rs) throws SQLException {
        return parseHome(h, rs, null);
    }

    public static Notice parseHome(Notice h, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            h.setNid(rs.getLong(su+"nid"));
        } catch (SQLException e) {
            h.setNid(null);
        }
        try {
            h.setRuid(rs.getLong(su+"ruid"));
        } catch (SQLException e) {
            h.setRuid(null);
        }
        try {
            String str = rs.getString(su+"content");
            if (str != null) {
                str = str.replaceAll("<", "&lt;");
                str = str.replaceAll(">", "&gt;");
            }
            h.setContent(str);
        } catch (SQLException e) {
            h.setContent(null);
        }
        try {
            h.setCdate(rs.getTimestamp(su+"cdate"));
        } catch (SQLException e) {
            h.setCdate(null);
        }
        
        return h;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, Notice h) {
        par.put("nid",h.getNid());
        par.put("ruid",h.getRuid());
        par.put("content",h.getContent());
        par.put("cdate",h.getCdate());
        
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, Notice h) {
        args.put("nid",h.getNid() );
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final Notice h) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(h),
                        new String[] { "nid" });
                parsePS(ps, h);
                return ps;
            }

        };
    }

    private static String parseSQL(Notice h) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into notice(");
        int count = 0;
        if (null != h.getRuid()) {
            sql.append("ruid,");
            count++;
        }
        if (null != h.getContent()) {
            sql.append("content,");
            count++;
        }
        if (null != h.getCdate()) {
            sql.append("cdate,");
            count++;
        }

        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, Notice h)
        throws SQLException {
        int count = 1;
        if (null != h.getRuid()) {
            ps.setLong(count++,h.getRuid());
        }
        if (null != h.getContent()) {
            ps.setString(count++,h.getContent());
        }
        if (null != h.getCdate()) {
            ps.setTimestamp(count++,h.getCdate());
        }
    }

}
